package QuanLyHoaDon;

import KiemTra.KiemTra;
import QuanLyKhachHang.KhachHang;
import QuanLyThuoc.Thuoc;
import java.awt.event.ItemEvent;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

public class ThemHoaDon extends javax.swing.JFrame {

    KhachHang kh = new KhachHang();
    Thuoc t = new Thuoc();
    HoaDon hd = new HoaDon();
    KiemTra ktHoaDon = new KiemTra();
    Vector vLay = new Vector();

    public ThemHoaDon() {
        setResizable(false);
        initComponents();
        try {
            vLay = KhachHang.xemDanhSachKhachHang();
            this.layTenKhachHang(vLay);

            vLay = Thuoc.xemThuoc();
            this.layTenThuoc(vLay);

            vLay = HoaDon.xemHoaDon();
            this.layMaHoaDon(vLay);


        } catch (SQLException ex) {
            Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //Lấy tên khách hàng
    public void layTenKhachHang(Vector vLay) {
        Vector tenKhachHang = new Vector();
        tenKhachHang.addElement("---Chọn tên khách hàng---");
        for (int i = 0; i < vLay.size(); i++) {
            kh = (KhachHang) vLay.get(i);
            tenKhachHang.addElement(kh.getTenKhachHang());
        }
        DefaultComboBoxModel modelKhachHang = new DefaultComboBoxModel(tenKhachHang);
        cbTenKhachHang.setModel(modelKhachHang);
    }

    //Lấy tên thuốc
    public void layTenThuoc(Vector vLay) {
        Vector tenThuoc = new Vector();
        tenThuoc.addElement("---Chọn tên thuốc---");
        for (int i = 0; i < vLay.size(); i++) {
            t = (Thuoc) vLay.get(i);
            tenThuoc.addElement(t.getTenThuoc());
        }
        DefaultComboBoxModel modelTenThuoc = new DefaultComboBoxModel(tenThuoc);
        cbTenThuoc.setModel(modelTenThuoc);
    }

    //Lấy mã hóa đơn
    public void layMaHoaDon(Vector vLay) {
        Vector mHoaDon = new Vector();
        mHoaDon.addElement("---Chọn mã hóa đơn---");
        for (int i = 0; i < vLay.size(); i++) {
            hd = (HoaDon) vLay.get(i);
            mHoaDon.addElement(hd.getMaHoaDon());
        }
        DefaultComboBoxModel modelMaHoaDon = new DefaultComboBoxModel(mHoaDon);
        cbMaHoaDon.setModel(modelMaHoaDon);
    }

    //Thêm dữ liệu vào bảng hóa đơn
    public void themHoaDon() {
        if (cbLoaiHoaDon.getSelectedItem().equals("---Chọn loại hóa đơn---")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn loại hóa đơn.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            cbLoaiHoaDon.requestFocus();
            return;
        }
        if (cbTenKhachHang.getSelectedItem().equals("---Chọn tên khách hàng---")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn tên khách hàng.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            cbTenKhachHang.requestFocus();
            return;
        }
        if (cbTinhTrangHoaDon.getSelectedItem().equals("---Chọn tình trạng Hóa đơn---")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn tình trạng hóa đơn.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            cbTinhTrangHoaDon.requestFocus();
            return;
        }
        if (!ktHoaDon.kiemTraSo(txtThue.getText().trim())) {
            JOptionPane.showMessageDialog(null, "Bạn nhập thuế sai.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            txtThue.setText("");
            txtThue.requestFocus();
            return;
        }
        if (!ktHoaDon.kiemTraNam(txtNgayTao.getText().trim())) {
            JOptionPane.showMessageDialog(null, "Bạn nhập năm ngày tạo sai.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            txtNgayTao.setText("");
            txtNgayTao.requestFocus();
            return;
        }
        if (!ktHoaDon.kiemTraNam(txtThoiHan.getText().trim())) {
            JOptionPane.showMessageDialog(null, "Bạn nhập năm thời hạn sai.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            txtThoiHan.setText("");
            txtThoiHan.requestFocus();
            return;
        }

        int kt = 0;

        hd.setLoaiHoaDon(cbLoaiHoaDon.getSelectedItem().toString().trim());

        //Lấy mã khách hàng
        Vector vMaKhachHang = new Vector();
        try {
            vMaKhachHang = KhachHang.xemDanhSachKhachHang();
        } catch (SQLException ex) {
            Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < vMaKhachHang.size(); i++) {
            kh = (KhachHang) vMaKhachHang.get(i);
            if (cbTenKhachHang.getSelectedItem().toString().equals(kh.getTenKhachHang().toString())) {
                hd.setMaKhachHang(kh.getMaKhachHang());
            }
        }

        float thue = Float.parseFloat(txtThue.getText().trim());
        hd.setThue(thue);
        hd.setTinhTrang(cbTinhTrangHoaDon.getSelectedItem().toString().trim());
        //Lấy ngày tạo
        String nt;
        nt = (String) cbNgaytao.getSelectedItem() + "/" + cbThangNgayTao.getSelectedItem() + "/" + txtNgayTao.getText().trim();
        t.setHanDung(nt);

        //Lấy thời hạn
        String th;
        th = (String) cbNgayThoiHan.getSelectedItem() + "/" + cbThangThoiHan.getSelectedItem() + "/" + txtThoiHan.getText().trim();
        hd.setThoiHan(th);
        try {
            kt = HoaDon.themHoaDon(hd);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Thêm mới hóa đơn thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = HoaDon.xemHoaDon();
                this.layMaHoaDon(vLay);

            } else {
                JOptionPane.showMessageDialog(this, "Thêm mới hóa đơn không thành công.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    //Thêm chi tiết hóa đơn
    public void themCTHoaDon() {
        if (cbMaHoaDon.getSelectedItem().equals("---Chọn mã hóa đơn---")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn mã hóa đơn.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            cbMaHoaDon.requestFocus();
            return;
        }
        if (cbTenThuoc.getSelectedItem().equals("---Chọn tên thuốc---")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn tên thuốc.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            cbTenThuoc.requestFocus();
            return;
        }
        if (txtSoLuong.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Bạn chưa nhập số lượng.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            txtSoLuong.requestFocus();
            return;
        }
        if (!ktHoaDon.kiemTraSo(txtSoLuong.getText().trim())) {
            JOptionPane.showMessageDialog(null, "Bạn nhập số lượng sai.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            txtSoLuong.setText("");
            txtSoLuong.requestFocus();
            return;
        }

        int kt = 0;

        int maHoaDon = Integer.parseInt(cbMaHoaDon.getSelectedItem().toString());
        hd.setMaHoaDon(maHoaDon);

        //Lấy mã thuốc

        Vector vMaThuoc = new Vector();
        try {
            vMaThuoc = Thuoc.xemThuoc();
        } catch (SQLException ex) {
            Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < vMaThuoc.size(); i++) {
            t = (Thuoc) vMaThuoc.get(i);
            if (cbTenThuoc.getSelectedItem().toString().equals(t.getTenThuoc().toString())) {
                hd.setMaThuoc(t.getMaThuoc());
            }
        }
        int soLuong = Integer.parseInt(txtSoLuong.getText());
        hd.setSoLuong(soLuong);


        try {
            kt = HoaDon.themCTHoaDon(hd);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Thêm mới bán thuốc thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);

            } else {
                JOptionPane.showMessageDialog(this, "Thêm mới bán thuốc không thành công.Mã hóa đơn và mã thuốc đã có.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //Trở lại
    public void troLai() {
        cbLoaiHoaDon.setSelectedIndex(0);
        cbMaHoaDon.setSelectedIndex(0);
        txtThue.setText("");
        cbNgayThoiHan.setSelectedIndex(0);
        cbNgaytao.setSelectedIndex(0);
        cbTenKhachHang.setSelectedIndex(0);
        cbTenThuoc.setSelectedIndex(0);
        cbThangNgayTao.setSelectedIndex(0);
        cbThangThoiHan.setSelectedIndex(0);
        cbTinhTrangHoaDon.setSelectedIndex(0);
        txtNgayTao.setText("");
        txtSoLuong.setText("");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cbLoaiHoaDon = new javax.swing.JComboBox();
        cbTenKhachHang = new javax.swing.JComboBox();
        txtThue = new javax.swing.JTextField();
        cbNgaytao = new javax.swing.JComboBox();
        cbThangNgayTao = new javax.swing.JComboBox();
        txtNgayTao = new javax.swing.JTextField();
        cbNgayThoiHan = new javax.swing.JComboBox();
        cbThangThoiHan = new javax.swing.JComboBox();
        txtThoiHan = new javax.swing.JTextField();
        cbTinhTrangHoaDon = new javax.swing.JComboBox();
        btKhachHangMoi = new javax.swing.JButton();
        btThemHoaDon = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        btTroLai = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cbTenThuoc = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cbMaHoaDon = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        btBanThuoc = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Bảng hóa đơn"));

        jLabel1.setText("Loại hóa đơn :");

        jLabel3.setText("Tên Khách Hàng :");

        jLabel5.setText("Thuế :");

        jLabel6.setText("Thời hạn :");

        jLabel7.setText("Ngày tạo :");

        jLabel8.setText("Tình trạng :");

        cbLoaiHoaDon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---Chọn loại hóa đơn---", "Xuất hàng", "Nhập hàng" }));

        cbTenKhachHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbNgaytao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", " " }));
        cbNgaytao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbNgaytaoActionPerformed(evt);
            }
        });

        cbThangNgayTao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", " " }));

        txtNgayTao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNgayTaoActionPerformed(evt);
            }
        });

        cbNgayThoiHan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", " " }));

        cbThangThoiHan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", " " }));

        cbTinhTrangHoaDon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---Chọn tình trạng Hóa đơn---", "Đã thanh toán", "Chưa thanh toán" }));
        cbTinhTrangHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTinhTrangHoaDonActionPerformed(evt);
            }
        });

        btKhachHangMoi.setText("Khách Hàng Mới");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbNgaytao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbThangNgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtNgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbNgayThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbThangThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(149, 149, 149))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbLoaiHoaDon, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbTenKhachHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btKhachHangMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtThue, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cbTinhTrangHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(cbLoaiHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btKhachHangMoi))
                    .addComponent(cbTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtThue, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbTinhTrangHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbNgaytao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbThangNgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNgayTao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbNgayThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbThangThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtThoiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
        );

        btThemHoaDon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add 1.png"))); // NOI18N
        btThemHoaDon.setText("Thêm hóa đơn");
        btThemHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemHoaDonActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/exit.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        btTroLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Go-back.png"))); // NOI18N
        btTroLai.setText("Trở lại");
        btTroLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTroLaiActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bán thuốc"));

        jLabel2.setText("Tên thuốc :");

        cbTenThuoc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel9.setText("Mã hóa đơn :");

        cbMaHoaDon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbMaHoaDon.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbMaHoaDonItemStateChanged(evt);
            }
        });

        jLabel4.setText("Số lượng :");

        btBanThuoc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Vial-Pills.png"))); // NOI18N
        btBanThuoc.setText("Bán thuốc");
        btBanThuoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBanThuocActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbTenThuoc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbMaHoaDon, 0, 195, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btBanThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(txtSoLuong)))))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel4, jLabel9});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(cbMaHoaDon, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbTenThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btBanThuoc)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/123.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btThemHoaDon)
                        .addGap(26, 26, 26)
                        .addComponent(btTroLai, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel10))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btThemHoaDon)
                            .addComponent(btTroLai)
                            .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNgayTaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNgayTaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNgayTaoActionPerformed

    private void cbTinhTrangHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTinhTrangHoaDonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbTinhTrangHoaDonActionPerformed

    private void btThemHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemHoaDonActionPerformed
        // TODO add your handling code here:
        this.themHoaDon();
    }//GEN-LAST:event_btThemHoaDonActionPerformed

    private void btBanThuocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBanThuocActionPerformed
        // TODO add your handling code here:
        this.themCTHoaDon();
    }//GEN-LAST:event_btBanThuocActionPerformed

    private void btTroLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTroLaiActionPerformed
        // TODO add your handling code here:
        this.troLai();
    }//GEN-LAST:event_btTroLaiActionPerformed

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        int thongBao = JOptionPane.showConfirmDialog(null, "Bạn có muốn đóng thêm hóa đơn?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (thongBao == JOptionPane.YES_OPTION) {
            this.dispose();
        }
    }//GEN-LAST:event_btThoatActionPerformed

    private void cbNgaytaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNgaytaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbNgaytaoActionPerformed

    private void cbMaHoaDonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbMaHoaDonItemStateChanged
        // TODO add your handling code here:

        if (evt.getStateChange() == ItemEvent.SELECTED) {
            Vector vHoaDon = new Vector();
            try {
                vHoaDon = HoaDon.xemHoaDon();
                for (int i = 0; i < vHoaDon.size(); i++) {
                    hd = (HoaDon) vHoaDon.get(i);

                    if (cbMaHoaDon.getSelectedItem() == hd.getMaHoaDon()) {
                     
                        if (hd.getLoaiHoaDon().toString().trim().equals("Xuất hàng")) {
                            cbLoaiHoaDon.setSelectedIndex(1);
                        }else{
                             cbLoaiHoaDon.setSelectedIndex(2);
                        }
                            

                        cbTenKhachHang.setSelectedItem(hd.getTenKhachHang());
                        String thue = Float.toString(hd.getThue());
                        txtThue.setText(thue);
                        cbTinhTrangHoaDon.setSelectedItem(hd.getTinhTrang());
                    }

                }
            } catch (SQLException ex) {
                Logger.getLogger(ThemHoaDon.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }//GEN-LAST:event_cbMaHoaDonItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ThemHoaDon().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBanThuoc;
    private javax.swing.JButton btKhachHangMoi;
    private javax.swing.JButton btThemHoaDon;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btTroLai;
    private javax.swing.JComboBox cbLoaiHoaDon;
    private javax.swing.JComboBox cbMaHoaDon;
    private javax.swing.JComboBox cbNgayThoiHan;
    private javax.swing.JComboBox cbNgaytao;
    private javax.swing.JComboBox cbTenKhachHang;
    private javax.swing.JComboBox cbTenThuoc;
    private javax.swing.JComboBox cbThangNgayTao;
    private javax.swing.JComboBox cbThangThoiHan;
    private javax.swing.JComboBox cbTinhTrangHoaDon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtNgayTao;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtThoiHan;
    private javax.swing.JTextField txtThue;
    // End of variables declaration//GEN-END:variables
}
